package com.food.eat.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author:RenPu
 * @Date: 2020/7/18 16:49
 * @Version: 1.0
 * @description:
 */

//@ApiIgnore    //此注解让swagger2忽略对此类，生成接口文档
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Resource
    private RedisTemplate redisTemplate;


    @GetMapping(value = "setKeyVlaue")
    public Object setKeyVlaue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "存储到redis数据成功！！！O(∩_∩)O哈哈~";
    }

    @GetMapping(value = "getKey")
    public Object getKey(String key) {
        String value = (String) redisTemplate.opsForValue().get(key);
        return "从redis取到的值为：" + value;
    }

    @GetMapping(value = "deleteKey")
    public Object deleteKey(String key) {
        redisTemplate.delete(key);
        return "删除redis的数据成功！！！！O(∩_∩)O哈哈~";
    }


    /**
     * spring session作为分布式会话的缺点：
     * 1：耦合度比较高，不能和redis进行脱离，数据结构复杂
     * 2：不方便其他语言的开发的镶嵌，例如php,python
     *
     * @param request
     * @return
     */

    @GetMapping(value = "springSession")
    public String getSpringSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("user", "new user Info");
        session.setMaxInactiveInterval(3600);
        return "ok";
    }


}
